﻿using System;
using System.Diagnostics;
using System.Reflection;

namespace SuiHan.Commons.Logging {
    public static class LoggerFactory {
        private static LoggerConfig _loggerConfig;

        public static Logger GetLogger(string category) {
            return new LoggerImpl(category) {Config = _loggerConfig};
        }

        public static Logger GetLogger(Type type) {
            return GetLogger(type.FullName);
        }

        public static Logger GetLogger<T>() {
            return GetLogger(typeof(T).FullName);
        }

        public static Logger GetCurrentLogger() {
            MethodBase method = new StackTrace().GetFrame(1).GetMethod();
            string category = method.ReflectedType!.FullName;
            return GetLogger(category);
        }

        /// <summary>
        /// 初始化日志配置
        /// </summary>
        /// <param name="config">LoggerConfig</param>
        public static void Config(LoggerConfig config) {
            _loggerConfig = config;
            if (config.FileEnabled && config.LogWriter == null) {
                config.LogWriter = new LogFileWriter(config);
            }

            // LoggerConsole.Install();
            // LoggerConsole.OutputEnabled = config.ConsoleEnabled;
        }
    }
}
